package org.rcy.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/10/10 14:29
 */

@Configuration
public class SpringMvcConfigurer implements WebMvcConfigurer {

	@Bean
	@Primary
	public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
		ObjectMapper mapper =  builder.createXmlMapper(false).timeZone(TimeZone.getTimeZone("Asia/Shanghai")).build();
		//忽略值为null的属性
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		SimpleModule module = new SimpleModule();
		module.addSerializer(Long.class, new ToStringSerializer());
		module.addSerializer(Date.class, new DateSerializer(false, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
		module.addDeserializer(Date.class, new DateDeserializers.DateDeserializer(new DateDeserializers.DateDeserializer(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss" ));
		mapper.registerModule(module);
		return mapper;
	}

	@Bean
	public BannerPathProvier bannerPathProvier(){
		BannerPathProvier provier = new BannerPathProvier().bulidPaths("https://i-blog.csdnimg.cn/blog_migrate/99b7cd44a9e6d1d97efa2bc432fa2be2.png")
				.bulidPaths("https://i-blog.csdnimg.cn/blog_migrate/f0fa516089b8ebb7e43cd5a55080ff27.jpeg")
				.bulidPaths("https://i-blog.csdnimg.cn/blog_migrate/459a2db750fd59ca43b295a961f3b314.jpeg")
				.bulidPaths("https://i-blog.csdnimg.cn/blog_migrate/459a2db750fd59ca43b295a961f3b314.jpeg");
		return provier;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
	}
}
